﻿using System;
/// <summary>
/// 定义类logger
/// 使用Action委托类型定义方法WriteMessage
/// 在LogMessage方法中，使用WriteMessage方法写日志。
/// </summary>

public enum Severity
{
    Verbose,
    Trace,
    Information,
    Warning,
    Error,
    Critical
}

public class Logger
{
    public Action<string> WriteMessage;

    public void LogMessage(Severity s, string component, string msg)
    {
        var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
        if (WriteMessage != null)
        {
            WriteMessage(outputMsg);
        }
    }
}

namespace 第三题
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Logger类，logger
            Logger logger = new Logger();

            //实例化屏幕显示日志类，c1
            ConsoleLogger cl = new ConsoleLogger();

            //实例化文件记录日志类，f1
            FileLogger fl = new FileLogger(@"F:\a\log.txt");

            //分别用c1和f1的方法，完成logger委托
            logger.WriteMessage += cl.LogToConsole;
            logger.WriteMessage += fl.LogToFile;

            //记录日志
            logger.LogMessage(Severity.Error, "program", "程序开始运行");

            //取消文件日志记录
            logger.WriteMessage -= fl.LogToFile;
            logger.LogMessage(Severity.Error, "program", "取消文件日志记录");

            //取消控制台日志记录
            logger.WriteMessage -= cl.LogToConsole;
            logger.LogMessage(Severity.Error, "program", "取消控制台日志记录");
        }
    }
}
